
package com.xifan.myaccount.util;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;
import android.util.DisplayMetrics;

import com.pinyin4android.PinyinUtil;
import com.xifan.myaccount.R;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class Util {

    public static final long IMAGE_MAX_SIZE = 1048576; // 1mb

    public static File getAppPath() {
        String root = Environment.getExternalStorageDirectory() + File.separator + "MaiBen";
        File path = new File(root);
        if (!path.exists()) {
            path.mkdir();
        }
        File nomedia = new File(root + File.separator + ".nomedia");
        if (!nomedia.exists()) {
            nomedia.mkdir();
        }
        return path;
    }

    /**
     * @return this year.
     */
    public static int getYear() {
        return getDate().get(Calendar.YEAR);
    }

    /**
     * @return month of the year, remember it's smaller than real month by 1.
     */
    public static int getMonth() {
        return getDate().get(Calendar.MONTH);
    }

    /**
     * @param month Specified month
     * @return Standard format of datetime with specified month,if month =
     *         -1,then return current month of date.
     */
    public static String getDatetimeByMonth(int month) {
        if (month == -1) {
            return getDatetime();
        } else {
            Calendar calendar = getDate();
            calendar.set(Calendar.MONTH, month);
            return getDateFormatter().format(calendar.getTime());
        }
    }

    /**
     * @return Standard format of datetime
     */
    public static String getDatetime() {
        SimpleDateFormat dateFomatter = getDateFormatter();
        return dateFomatter.format(getDate().getTime());
    }

    public static int getDayOfTime() {
        return getDate().get(Calendar.DAY_OF_MONTH);
    }

    public static int getHourOfTime() {
        return getDate().get(Calendar.HOUR_OF_DAY);
    }

    public static int getMinuteOfTime() {
        return getDate().get(Calendar.MINUTE);
    }

    public static long getSecondsNow() {
        return getDate().getTimeInMillis();
    }

    public static int getDaysFromNow(String date) {
        return getDateFromNow(date).get(Calendar.DATE);
    }

    public static int getMinutesFromNow(String dateStr) {
        return getDateFromNow(dateStr).get(Calendar.MINUTE);
    }

    public static Calendar getDateFromNow(String dateStr) {
        try {
            SimpleDateFormat sdf = getDateFormatter();

            Date dateNow = getDate().getTime();
            Date date = sdf.parse(dateStr);

            Calendar calNow = getDate();
            calNow.setTimeInMillis(dateNow.getTime() - date.getTime());
            return calNow;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Calendar parseToDate(String dateStr) {
        try {
            SimpleDateFormat sdf = getDateFormatter();
            Date date = sdf.parse(dateStr);
            Calendar cal = getDate();
            cal.setTime(date);
            return cal;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Calendar getDate() {
        return Calendar.getInstance();
    }

    public static SimpleDateFormat getDateFormatter() {
        return getDateFormatter("yyyy-MM-dd HH:mm");
    }

    public static SimpleDateFormat getDateFormatter(String format) {
        return new SimpleDateFormat(format, Locale.getDefault());
    }

    public static int getCurrentAccountId(Context context) {
        SharedPreferences prefs = context.getSharedPreferences(SettingsProvider.PREF_NAME, 0);
        int account = prefs.getInt(SettingsProvider.PREF_CURRENT_ACCOUNT, -1);
        if (account < 1) {
            // default use Cash account
            account = 1;
            prefs.edit().putInt(SettingsProvider.PREF_CURRENT_ACCOUNT, 1).apply();
        }
        return account;
    }

    public static boolean isChinese(String s) {
        return s.length() > 0 ? s.substring(0, 1).matches("[\u4E00-\u9FA5]") : false;
    }

    public static String getPinyin(Context context, CharSequence s) {
        String[] tmp = PinyinUtil.toPinyin(context, s.toString()).split(" ");
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < tmp.length; i++) {
            str.append(tmp[i].substring(0, 1));
        }
        return str.toString().trim();

    }

    public static DisplayMetrics getDisplayMetrics(Activity activity) {
        DisplayMetrics metrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        return metrics;
    }

    public static String getLocation(Context context) {
        SharedPreferences pref = context.getSharedPreferences("pref", 0);
        return pref.getString("location",
                context.getResources().getString(R.string.location_default_name));
    }

    public static String[] getLocationList(Context context) {
        GeocodeHelper geo = new GeocodeHelper(context);
        String[] list = geo.getGeoLocations();
        if (list == null) {
            list = new String[1];
            list[0] = context.getResources().getString(R.string.location_default_name);
        }
        return list;
    }

}
